
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_data_json_app/entity/request_bean.dart';

import 'base_bean.dart';
import '../data.dart';
import '../http/my_dio.dart';

Future<RequestBean<T>> postWithBean<T extends BaseBean>(T t,String s,dynamic data,{bool showLoading:true,bool canCancel:true}) async {
  if(s.isEmpty){
    return RequestBean.error(404,'请求方法不可为空');
  }
  Response<dynamic> response=await DioUtil.instance.post(s, data,showDialog: showLoading,canCancel: canCancel);
  if(response!=null){
    print(response);
    if(response.statusCode==HttpStatus.OK){
      if (response.data['code']==0) {
        return RequestBean.success(response.data['code'], t.fromJson(response.data['data']));
      }else if(response.data['code']==-1){
        return RequestBean.error(response.data['code'], response.data['msg']);
      }else{
        return RequestBean.error(response.data['code'], response.data['msg']);
      }
    }else{
      return RequestBean.error(404, "请检查网络");
    }
  }else{
    return RequestBean.error(400, "请求出错");
  }
}

Future<RequestBean> postWithDynamic(String s,dynamic data,{bool showLoading:true,bool canCancel:true})async{
  if(s.isEmpty){
    return RequestBean.error(404,'请求方法不可为空');
  }
//  这里使用StaticValue中的context作为弹窗用context
  Response<dynamic> response=await DioUtil.instance.post(s, data,showDialog: showLoading,canCancel: canCancel);
  print(response);
  if(response!=null){
    if(response.statusCode==HttpStatus.OK){
      if (response.data['code']==0) {
        return RequestBean.success(response.data['code'], response.data['data']);
      }else if(response.data['code']==-1){
        return RequestBean.error(response.data['code'], response.data['msg']);
      }else{
        return RequestBean.error(response.data['code'], response.data['msg']);
      }
    }else{
      return RequestBean.error(404, "请检查网络");
    }
  }else{
    return RequestBean.error(400, "请求出错");
  }
}